import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import legacy from "@vitejs/plugin-legacy"

// https://vite.dev/config/
export default defineConfig({
  base: './',
  build: {
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          const fileName = assetInfo.name || 'unknown'
          const extType = fileName.split('.').pop()?.toLowerCase() || ''

          if (/png|jpe?g|svg|gif|webp|avif|ico/i.test(extType)) {
            return 'assets/images/[name]-[hash][extname]'
          }
          if (/css|s[ac]ss/i.test(extType)) {
            return 'assets/css/[name]-[hash][extname]'
          }
          return 'assets/other/[name]-[hash][extname]'
        }
      }
    }
  },
  plugins: [
    vue(),
    vueDevTools(),
    legacy({
      targets: ["ie>=11"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
})
